package org.erp.store.service.impl;

import lombok.extern.log4j.Log4j2;
import org.erp.store.domain.dto.BatchDeviceInventoryDto;
import org.erp.store.domain.dto.BatchDeviceInventoryResponseDto;
import org.erp.store.service.BatchDeviceInventoryService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

import org.erp.store.strategy.InventoryEventStrategy;
import org.erp.store.router.InventoryEventRouter;
/**
 * 设备库存批量状态更新服务实现
 *
 * @author dingding
 * @since 2025-11-12
 */
@Log4j2
@Service
public class BatchDeviceInventoryServiceImpl implements BatchDeviceInventoryService {

    @Resource
    private InventoryEventRouter inventoryEventRouter;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public BatchDeviceInventoryResponseDto batchUpdateStatusStrategic(BatchDeviceInventoryDto dto) {
        long startTime = System.currentTimeMillis();
        InventoryEventStrategy strategy = inventoryEventRouter.get(dto.getTargetEvent());
        Assert.isTrue(strategy != null, "不支持的事件类型");
        log.info("当前事件：",dto.getTargetEvent().getMessage());
        BatchDeviceInventoryResponseDto response = strategy.execute(dto);
        long costTime = System.currentTimeMillis() - startTime;
        log.info("批量更新设备库存状态完成，数量:{}, 耗时:{}ms, 成功率:{}/{}", response.getSuccessCount(), costTime, response.getSuccessCount(),response.getTotalCount());
        return response;
    }
}
